// src/yu/LoginServlet.java
package yu;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        boolean isValid = new UserService().validate(username, password);

        if (isValid) {
            req.getSession().setAttribute("user", username);
            resp.sendRedirect("welcome.html");
        } else {
            req.setAttribute("error", "密码错误！");
            req.getRequestDispatcher("login.html").forward(req, resp);
        }
        // 在LoginServlet的doPost方法中添加：
        String remember = req.getParameter("remember");
        if ("on".equals(remember)) {
            Cookie cookie = new Cookie("autoLogin", username);
            cookie.setMaxAge(7 * 24 * 60 * 60); // 7天有效期
            resp.addCookie(cookie);
        }
    }
}
